package com.yuwen.example.provider;

/**
 * @ClassNameProviderExample
 * @Description TODO
 * @Author钰玟
 * @Date2025/3/30 下午4:26
 * @Version 1.0
 **/

import com.yuwen.RpcApplication;
import com.yuwen.example.common.service.UserService;
import com.yuwen.yuwenRPC.bootstrap.ProviderBootstrap;
import com.yuwen.yuwenRPC.config.RegistryConfig;
import com.yuwen.yuwenRPC.config.RpcConfig;
import com.yuwen.yuwenRPC.model.ServiceMetaInfo;
import com.yuwen.yuwenRPC.model.ServiceRegistryInfo;
import com.yuwen.yuwenRPC.registry.Registry;
import com.yuwen.yuwenRPC.registry.RegistryFactory;
import com.yuwen.yuwenRPC.server.HttpServer;
import com.yuwen.yuwenRPC.server.VertxHttpServer;
import com.yuwen.yuwenRPC.server.tcp.VertxTcpServer;

import java.util.ArrayList;
import java.util.List;

import static com.yuwen.yuwenRPC.constant.RpcConstant.DEFAULT_SERVICE_VERSION;
import static com.yuwen.yuwenRPC.registry.LocalRegistry.registry;

/**
 * 简易服务提供者  示例
 */
public class ProviderExample {
    public static void main(String[] args) {
        /**
         * 要注册的服务
         */
        List<ServiceRegistryInfo<?>> serviceRegistryInfoList = new ArrayList<>();
        ServiceRegistryInfo serviceRegistryInfo = new ServiceRegistryInfo(UserService.class.getName(),UserServiceImpl.class);
        serviceRegistryInfoList.add(serviceRegistryInfo);
        /**
         *服务提供者初始化
         */
        ProviderBootstrap.init(serviceRegistryInfoList);
    }


    @Deprecated
    public static void DeprecatedMain(){
        /**
         * rpc框架初始化
         */
        RpcApplication.init();
        /**
         * 注册服务
         */

        registry(UserService.class.getName(),UserServiceImpl.class);

        /**
         * 注册 服务元信息 至注册中心
         */
        RpcConfig rpcConfig = new RpcConfig();
        String registryType = rpcConfig.getRegistryConfig().getRegistryType();
        Registry registry = RegistryFactory.getRegistry(registryType);
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(UserService.class.getName());
        serviceMetaInfo.setServiceHost(rpcConfig.getHost());
        serviceMetaInfo.setServiceVersion(DEFAULT_SERVICE_VERSION);
        serviceMetaInfo.setServicePort(rpcConfig.getPort());
        try {
            registry.register(serviceMetaInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //提供服务  启动web服务
        /**
         * provider也需要引入 vertX 的依赖
         */

//        启动web服务
//        HttpServer httpServer = new VertxHttpServer();
//
//        httpServer.doStart(RpcApplication.getRpcConfig().getPort());


        //启动tcp服务
        VertxTcpServer vertxTcpServer = new VertxTcpServer();

        vertxTcpServer.doStart(8080);
    }
}
